/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exemplo1;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author g0006185
 */
public class ContatoDAO {
   
    private Connection cn;
    private static final String inssql = "insert into contatos (nome,email,endereco,dataNascimento) values(?,?,?,?)";
    private static final String selsql = "select * from contatos";
    private static final String altsql = "update contatos set nome=?,email=?,endereco=?,datanascimento=? where id =?;";
    private static final String delsql = "delete from contatos where id=?";
    public ContatoDAO() throws FileNotFoundException, ClassNotFoundException, IOException, SQLException {
        this.cn =  new ConnectionFaactory().getConnection();
    }
    public void adiciona(ContatoDto contato) throws SQLException{
        PreparedStatement stmt = cn.prepareStatement(inssql);
        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
        stmt.execute();
        stmt.close();
        cn.close();
    }  
    public List<ContatoDto> findContato() throws SQLException, ParseException{
         List<ContatoDto> lista = new ArrayList();
        PreparedStatement stmt = cn.prepareStatement(selsql);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            ContatoDto ct = new ContatoDto();
            ct.setId(rs.getLong("id"));
            ct.setNome(rs.getString("nome"));
            ct.setEndereco(rs.getString("endereco"));
            ct.setEmail(rs.getString("email"));
            String dt = rs.getString("dataNascimento");
             SimpleDateFormat dtp = new SimpleDateFormat("yyyy-MM-dd"); 
             Calendar date = Calendar.getInstance();
             date.setTime(dtp.parse(dt));
            ct.setDataNascimento(date);
            lista.add(ct);
        }
        rs.close();
        stmt.close();
        cn.close();
        return lista;
        
    }
    public void alteracontato(ContatoDto contato) throws SQLException{
        PreparedStatement stmt = cn.prepareStatement(altsql);
        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
        stmt.setLong(5, contato.getId());
        stmt.execute();
        stmt.close();
        cn.close();
    }
    public void delcontato(Long id) throws SQLException{
        PreparedStatement stmt = cn.prepareStatement(delsql);
        stmt.setLong(1, id);
        stmt.execute();
        stmt.close();
        cn.close();
    }
}
